export interface PaymentOrder {
  id: string
  merchantId: string
  merchantName: string
  amount: number
  currency: 'CNY' | 'USD'
  status: 'pending' | 'success' | 'failed' | 'cancelled'
  paymentMethod: 'alipay' | 'wechat' | 'bank'
  paymentChannel: string
  transactionId?: string
  createTime: string
  updateTime: string
  completedTime?: string
  remark?: string
}

// 生成指定范围内的随机数
const getRandomNumber = (min: number, max: number) => {
  return Math.floor(Math.random() * (max - min + 1)) + min
}

// 生成模拟数据
export const generateMockData = (count: number = 50): PaymentOrder[] => {
  const data: PaymentOrder[] = []
  const statuses: PaymentOrder['status'][] = ['pending', 'success', 'failed', 'cancelled']
  const paymentMethods: PaymentOrder['paymentMethod'][] = ['alipay', 'wechat', 'bank']
  const channels = {
    alipay: ['支付宝官方', '支付宝服务商'],
    wechat: ['微信官方', '微信服务商'],
    bank: ['工商银行', '建设银行', '农业银行']
  }

  // 添加一些示例数据
  data.push(
    {
      id: 'P202506031200001',
      merchantId: 'M2025060311001',
      merchantName: '测试商户01',
      amount: 100.00,
      currency: 'CNY',
      status: 'success',
      paymentMethod: 'alipay',
      paymentChannel: '支付宝官方',
      transactionId: '2025060312000000001',
      createTime: '2025/6/3 12:00:00',
      updateTime: '2025/6/3 12:00:30',
      completedTime: '2025/6/3 12:00:30',
      remark: '测试支付订单'
    },
    {
      id: 'P202506031200002',
      merchantId: 'M2025060311002',
      merchantName: '测试商户02',
      amount: 50.00,
      currency: 'CNY',
      status: 'pending',
      paymentMethod: 'wechat',
      paymentChannel: '微信官方',
      createTime: '2025/6/3 12:01:00',
      updateTime: '2025/6/3 12:01:00',
      remark: '待支付订单'
    }
  )

  // 生成更多模拟数据
  for (let i = 3; i <= count; i++) {
    const status = statuses[Math.floor(Math.random() * statuses.length)]
    const paymentMethod = paymentMethods[Math.floor(Math.random() * paymentMethods.length)]
    const channel = channels[paymentMethod][Math.floor(Math.random() * channels[paymentMethod].length)]
    const amount = getRandomNumber(10, 10000) / 100
    const createDate = new Date(2025, 5, getRandomNumber(1, 30), getRandomNumber(0, 23), getRandomNumber(0, 59))
    const updateDate = new Date(createDate.getTime() + getRandomNumber(0, 3600000)) // 最多1小时后更新

    const order: PaymentOrder = {
      id: `P${createDate.getFullYear()}${String(createDate.getMonth() + 1).padStart(2, '0')}${String(createDate.getDate()).padStart(2, '0')}${String(i).padStart(6, '0')}`,
      merchantId: `M${createDate.getFullYear()}${String(createDate.getMonth() + 1).padStart(2, '0')}${String(createDate.getDate()).padStart(2, '0')}${String(getRandomNumber(1000, 9999))}`,
      merchantName: `商户${i}`,
      amount,
      currency: 'CNY',
      status,
      paymentMethod,
      paymentChannel: channel,
      createTime: createDate.toLocaleString(),
      updateTime: updateDate.toLocaleString(),
      remark: `支付订单${i}`
    }

    if (status === 'success') {
      order.transactionId = `${createDate.getFullYear()}${String(createDate.getMonth() + 1).padStart(2, '0')}${String(createDate.getDate()).padStart(2, '0')}${String(getRandomNumber(100000000, 999999999))}`
      order.completedTime = updateDate.toLocaleString()
    }

    data.push(order)
  }

  return data
}

export const mockData = generateMockData()

// 增删改查
export function addPaymentOrder(item: PaymentOrder) {
  mockData.unshift(item)
}

export function updatePaymentOrder(id: string, item: Partial<PaymentOrder>) {
  const idx = mockData.findIndex(m => m.id === id)
  if (idx > -1) Object.assign(mockData[idx], item)
}

export function deletePaymentOrder(id: string) {
  const idx = mockData.findIndex(m => m.id === id)
  if (idx > -1) mockData.splice(idx, 1)
}

export function getPaymentOrder(id: string): PaymentOrder | undefined {
  return mockData.find(m => m.id === id)
}

// 状态映射
export const statusMap = {
  pending: { text: '待支付', color: 'orange' },
  success: { text: '支付成功', color: 'success' },
  failed: { text: '支付失败', color: 'error' },
  cancelled: { text: '已取消', color: 'default' }
}

// 支付方式映射
export const paymentMethodMap = {
  alipay: { text: '支付宝', icon: 'alipay' },
  wechat: { text: '微信支付', icon: 'wechat' },
  bank: { text: '银行卡', icon: 'bank' }
}